1
/************************************* Module Header **************************************\
2 * Module Name: GetWrapperForm.cs
3 * Project: CSVstoGetWrapperObject
4 * Copyright (c) Microsoft Corporation.
6 * The CSVstoGetWrapperObject project demonstrates how to get a VSTO wrapper
7 * object from an existing Office COM object.
9 * This feature requires Visual Studio Tools for Office 3.0 SP1 (included in
10 * Visual Studio 2008 SP1) for both design-time and runtime support.
12 * This source is subject to the Microsoft Public License.
13 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
14 * All other rights reserved.
17 * * 6/13/2009 3:00 PM Jie Wang Created
18 \******************************************************************************************/
20 #region Using directives
22 using System
.Collections
.Generic
;
23 using System
.ComponentModel
;
28 using System
.Windows
.Forms
;
29 using Excel
= Microsoft
.Office
.Interop
.Excel
;
30 using Microsoft
.Office
.Tools
.Excel
;
31 using Microsoft
.Office
.Tools
.Excel
.Extensions
;
32 using System
.Runtime
.InteropServices
;
33 using Microsoft
.VisualStudio
.Tools
.Applications
.Runtime
;
37 namespace CSVstoGetWrapperObject
39 public partial class GetWrapperForm
: Form
41 public GetWrapperForm()
43 InitializeComponent();
46 private void GetWrapperForm_Load(object sender
, EventArgs e
)
48 btnRefreshWb
.PerformClick();
52 /// Refresh the Workbook list.
54 /// <param name="sender"></param>
55 /// <param name="e"></param>
56 private void btnRefreshWb_Click(object sender
, EventArgs e
)
58 cboWorkbooks
.Items
.Clear();
60 foreach (Excel
.Workbook wb
in Globals
.ThisAddIn
.Application
.Workbooks
)
62 cboWorkbooks
.Items
.Add(wb
);
65 bool hasWorkbook
= cboWorkbooks
.Items
.Count
> 0;
67 cboWorksheets
.Enabled
= hasWorkbook
;
68 btnRefreshWs
.Enabled
= hasWorkbook
;
69 btnAddListObject
.Enabled
= hasWorkbook
;
73 cboWorkbooks
.SelectedIndex
= 0;
78 /// Workbook selection changed, refresh the Workseet list.
80 /// <param name="sender"></param>
81 /// <param name="e"></param>
82 private void cboWorkbooks_SelectedIndexChanged(object sender
, EventArgs e
)
84 if (cboWorkbooks
.SelectedItem
!= null)
86 btnRefreshWs
.PerformClick();
91 /// Refresh the Worksheet list.
93 /// <param name="sender"></param>
94 /// <param name="e"></param>
95 private void btnRefreshWs_Click(object sender
, EventArgs e
)
97 Excel
.Workbook wb
= (Excel
.Workbook
)cboWorkbooks
.SelectedItem
;
98 cboWorksheets
.Items
.Clear();
100 foreach (Excel
.Worksheet ws
in wb
.Worksheets
)
102 cboWorksheets
.Items
.Add(ws
);
105 cboWorksheets
.SelectedIndex
= 0;
111 /// <param name="sender"></param>
112 /// <param name="e"></param>
113 private void btnAddListObject_Click(object sender
, EventArgs e
)
115 // This is Microsoft.Office.Interop.Excel.Worksheet (COM)
116 Excel
.Worksheet ws
= (Excel
.Worksheet
)cboWorksheets
.SelectedItem
;
119 // This is Microsoft.Office.Tools.Excel.Worksheet (VSTO wrapper)
120 Worksheet vstoWs
= Worksheet
.GetVstoObject(ws
);
124 // Now we have the VSTO wrapper, add some VSTO objects to it...
125 // First a ListObject
126 ListObject lo
= vstoWs
.Controls
.AddListObject(vstoWs
.Range
["A3", Type
.Missing
], "myTable");
127 // Try bind some data to the ListObject
128 lo
.DataSource
= GetDemoData();
129 lo
.DataMember
= "DemoTable";
132 Button btnVsto
= vstoWs
.Controls
.AddButton(vstoWs
.Range
["A1", Type
.Missing
], "btnVSTO");
133 btnVsto
.Text
= "VSTO Button";
136 // Setup the button Click event handler.
137 btnVsto
.Click
+= delegate(object s
, EventArgs args
)
139 MessageBox
.Show("VSTO button clicked.", "GetVstoObject demo", MessageBoxButtons
.OK
, MessageBoxIcon
.Information
);
142 catch (RuntimeException rtEx
)
144 MessageBox
.Show(rtEx
.ToString(), "GetVstoObject demo", MessageBoxButtons
.OK
, MessageBoxIcon
.Error
);
149 /// Generates some data for ListObject databinding.
151 /// <returns></returns>
152 private DemoData
GetDemoData()
154 DemoData data
= new DemoData();
156 data
.DemoTable
.Rows
.Add(new object[] {null, "John", new DateTime(1978, 2, 20)}
);
157 data
.DemoTable
.Rows
.Add(new object[] { null, "Eric", new DateTime(1987, 6, 12) }
);
158 data
.DemoTable
.Rows
.Add(new object[] { null, "Mary", new DateTime(1980, 8, 10) }
);
159 data
.DemoTable
.Rows
.Add(new object[] { null, "Mike", new DateTime(1991, 1, 9) }
);
160 data
.DemoTable
.Rows
.Add(new object[] { null, "Joe", new DateTime(1983, 3, 31) }
);
161 data
.DemoTable
.Rows
.Add(new object[] { null, "Lance", new DateTime(1988, 5, 11) }
);
162 data
.DemoTable
.Rows
.Add(new object[] { null, "Tom", new DateTime(1970, 9, 30) }
);